/*
 * FileBrowserDialog.java -
 * Copyright (c) 2005 Carmine Lia
 * e-mail:      carmine.lia@libero.it
 * web-address: http://www.intermedia.sa.it/lia

 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
package javax.robotics.swing;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.robotics.xml.XmlFileChooser;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 * Dialog panel to select a remote or local file.
 * 
 * @author <a href="http://www.intermedia.sa.it/lia/">Carmine Lia</a>
 * @since 1.1.2
 * @version 04/11/2005
 */
public final class FileBrowserDialog extends JOptionPane
{

    /**
     * 
     */
    private static final long serialVersionUID = -5537955766112564039L;
    private URI uri = null;

    /**
     * Dialog Constructor.
     * 
     * @param title
     *            the message shown in the window.
     * @param url
     *            the url of the file.
     * @throws IOException
     * @throws MalformedURLException
     * @throws URISyntaxException
     */
    public FileBrowserDialog(String title, String url) throws IOException,
            MalformedURLException, URISyntaxException
    {
        super(
                "The URI or URL of configuration file");
        final Object options[] =
            { "Open", "Browse local", "Cancel" };

        this.setOptions(options);
        this.setWantsInput(true);
        this.setInitialSelectionValue(url);
        this.setInitialValue(options[0]);
        this.setMessageType(QUESTION_MESSAGE);
        showDialog(title);

    }

    private void showDialog(String title) throws URISyntaxException,
            IOException
    {
        JDialog dialog = this.createDialog(this, title);
        dialog.setSize(400, 200);
        dialog.setVisible(true);
        Object selectedValue = this.getValue();
        
        if(selectedValue == null)
        {
            uri = null;
            return;
        }
        
        int selectedIndex = 0;
        for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++)
        {
            if(options[counter].equals(selectedValue))
            {
                selectedIndex = counter;
                break;
            }
        }

        switch (selectedIndex)
        {
        case 0:
            // open
            uri = new URI((String) this.getInputValue());
            break;
        case 1:
            // browse local
            final XmlFileChooser fc = new XmlFileChooser();
            uri = fc.getURI();
            if(uri != null)
                this.setInitialSelectionValue(uri.toString());
            else
                this.setInitialSelectionValue("");
            showDialog(title);
            break;
        case 2:
            // cancel
            uri = null;
            break;

        default:
            uri = null;
            break;
        }
    }
    /**
     * Gets the URI file.
     * 
     * @return the file
     */
    public final URI getFile()
    {
        return uri;
    }
}
